package org.winexpress

import grails.converters.deep.JSON
import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;


class CatalogueManagementController {
    
    // reusable methods available, FIND with term 'reusable'
    
    def getCountryList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/country").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No countries.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<Country> countryList = new ArrayList<Country>()

            arr.each()
            {
                Country c = new Country()
                c.id = it.id
                c.countryName = it.countryName
                countryList.add(c)
            }

            return countryList
        }
    }
    
    def getSingleCountry(id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/country/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No countries.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                Country c = new Country()
                c.id = o.get("id")
                c.countryName = o.get("countryName")
                
                return c
            }
        }
        else {
            flash.message = 'No specified country found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    def getWineTypeList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineType/").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine types.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<WineType> wineTypeList = new ArrayList<WineType>()

            arr.each()
            {
                WineType wt = new WineType()
                wt.id = it.id
                wt.wineTypeName = it.wineTypeName
                wt.description = it.description
                wineTypeList.add(wt)
            }

            return wineTypeList
        }
    }
    
    def getSingleWineType(id) {
        String input = id
        String noSpaces = input.replaceAll(" ", "%20")
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineType/" + noSpaces).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine types.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                WineType wt = new WineType()
                wt.id = o.get("id")
                wt.wineTypeName = o.get("wineTypeName")
                wt.description = o.get("description")
                
                return wt
            }
        }
        else {
            flash.message = 'No specified wine type found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    def getWineVarietalList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineVarietal").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine varietals.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<WineVarietal> wineVarietalList = new ArrayList<WineVarietal>()

            arr.each()
            {
                WineVarietal wv = new WineVarietal()
                wv.id = it.id
                wv.wineVarietalName = it.wineVarietalName
                wv.description = it.description
                
                WineType wt = it.wineType
                wv.wineTypeName = wt.wineTypeName
                wineVarietalList.add(wv)
            }

            return wineVarietalList
        }
    }
    
    def getSingleWineVarietal(id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineVarietal/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine varietals.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                WineVarietal wv = new WineVarietal()
                wv.id = o.get("id")
                wv.wineVarietalName = o.get("wineVarietalName")
                wv.description = o.get("description")
                
//                WineType wt = getSingleWineType(o.get("wineType"))
//                wt.wineTypeName = wt.wineTypeName
                WineType wt = o.get("wineType")
                wv.wineTypeName = wt.wineTypeName
                
                return wv
            }
        }
        else {
            flash.message = 'No specified wine varietal found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    def getWineRegionList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineRegion/").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine regions.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<WineRegion> wineRegionList = new ArrayList<WineRegion>()

            arr.each()
            {
                WineRegion wr = new WineRegion()
                wr.id = it.id
                wr.wineRegionName = it.wineRegionName
                wr.description = it.description
                
                Country c = getSingleCountry(it.countryId)
                wr.countryName = c.countryName
                wineRegionList.add(wr)
            }

            return wineRegionList
        }
    }
    
    def getSingleWineRegion(id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineRegion/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine regions.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                WineRegion wr = new WineRegion()
                wr.id = o.get("id")
                wr.wineRegionName = o.get("wineRegionName")
                wr.description = o.get("description")
                
                Country c = getSingleCountry(o.get("countryId"))
                wr.countryName = c.countryName
                
                return wr
            }
        }
        else {
            flash.message = 'No specified wine region found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    def getProductTypeList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/productType/").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No product types.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<ProductType> productTypeList = new ArrayList<ProductType>()

            arr.each()
            {
                ProductType pt = new ProductType()
                pt.id = it.id
                pt.productTypeName = it.productTypeName
                pt.description = it.description
                productTypeList.add(pt)
            }

            return productTypeList
        }
    }
    
    def getSingleProductType(id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/productType/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No product types.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                ProductType pt = new ProductType()
                pt.id = o.get("id")
                pt.productTypeName = o.get("productTypeName")
                pt.description = o.get("description")
                
                return pt
            }
        }
        else {
            flash.message = 'No specified product type found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    def getProductList() {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/product/").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No products.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<Product> productList = new ArrayList<Product>()

            arr.each()
            {
                Product p = new Product()
                p.id = it.id
                p.productName = it.productName
                p.description = it.description
                p.imageUrl = it.imageUrl
                
                ProductType pt = it.productType
                p.productTypeName = pt.productTypeName
                
                productList.add(p)
            }

            return productList
        }
    }
    
    def getSingleProduct(id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/product/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No specified product found.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                Product p = new Product()
                p.id = o.get("id")
                p.productName = o.get("productName")
                p.description = o.get("description")
                p.imageUrl = o.get("imageUrl")
                
                ProductType pt = o.get("productType")
                p.productTypeName = pt.productTypeName
                
                return p
            }
        }
        else {
            flash.message = 'No specified product found.<br/><br/>'
            redirect(uri:"/")
        }
    }

    def getProductOptionList(productId) {
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/product/" + productId + "/productOption").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No product options.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<ProductOption> productOptionList = new ArrayList<ProductOption>()

            arr.each()
            {
                ProductOption po = new ProductOption()
                po.id = it.id
                po.optionType = it.optionType
                po.optionTypeId = it.optionTypeId
                po.productId = it.productId
                
                productOptionList.add(po)
            }

            return productOptionList
        }
    }
    
    def getSingleProductOption(productId, id) {
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/productOption/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No product options.<br/><br/>'
                    try {
                        redirect(uri:"/404")
                    }
                    catch (CannotRedirectException) {
                        println 'already redirected...'
                    }
                }
            }
            else {
                ProductOption po = new ProductOption()
                po.id = o.get("id")
                po.optionType = o.get("optionType")
                po.optionTypeId = o.get("optionTypeId")
                po.productId = o.get("productId")
                
                return po
            }
        }
        else {
            flash.message = 'No specified product option found.<br/><br/>'
            redirect(uri:"/")
        }
    }
    
    
    

    // initialization: GSP support operations (must be same as GSP filename)
    /* Wine Type */
    def wineTypeCreate = { 
        println 'wine type create '
    }
    
    def wineTypeRetrieve = { 
        println 'wine type Retrieve '
       
        [wineTypes:getWineTypeList()]
    }
    
    def wineTypeUpdate = { 
        println 'wine type update '
        
        def id = params['id']
        
        WineType wineType = getSingleWineType(id)
        
        [wineTypeId:wineType.id, wineTypeName:wineType.wineTypeName, wineTypeDescription:wineType.description]
    }
    
    def wineTypeDelete = { 
        println 'wine type delete '
    }
    
    
    /* Wine Varietal */
    def wineVarietalCreate = { 
        println 'wine var create '
        
        [wineTypes:getWineTypeList()]
    }
    
    def wineVarietalRetrieve = { 
        println 'wine var Retrieve '
        
        [wineVarietals:getWineVarietalList()]
    }
    
    def getWineVarietalsOfWineType = {
        println 'getWineVarietalsOfWineType '
        def wineTypeName = params['wineTypeName']
        wineTypeName = wineTypeName.replaceAll("%20", " ")
        WineType wineType = getSingleWineType(wineTypeName)
        ArrayList wineVarietals = new ArrayList()
        def currentWineVarietalList = getWineVarietalList()
        currentWineVarietalList.each() {           
            if (it.wineTypeName.equals(wineTypeName)) {
                wineVarietals.add(it)
            }
        } 
        render(template:"wineVarietalTemplate", model: ['wineVarietalList' : wineVarietals])  
    }
    
    def wineVarietalUpdate = { 
        println 'wine var update '
        
        def id = params['id']
        
        WineVarietal wv = getSingleWineVarietal(id)
        
        [wineVarietalId:wv.id, wineVarietalName:wv.wineVarietalName, wineVarietalDescription:wv.description, wineTypeName:wv.wineTypeName, wineTypes:getWineTypeList()]
        
    }
    
    def wineVarietalDelete = { 
        println 'wine var delete '
    }
    
    
    /* Wine Region */
    def wineRegionCreate = { 
        println 'wine Region create '
        
        [countries:getCountryList()]
    }
    
    def wineRegionRetrieve = { 
        println 'wine Region Retrieve '
        
        [wineRegions:getWineRegionList()]
    }
    
    def wineRegionUpdate = { 
        println 'wine Region update '
        
        def id = params['id']
        
        WineRegion wr = getSingleWineRegion(id)
        
        [wineRegionId:wr.id, wineRegionName:wr.wineRegionName, wineRegionDescription:wr.description, country:wr.countryName]
    }
    
    def wineRegionDelete = { 
        println 'wine Region delete '
    }
    
    
    /* Product Type */
    def productTypeCreate = { 
        println 'product type create ffff'
    }
    
    def productTypeRetrieve = { 
        println 'product type Retrieve '
       
        [productTypes:getProductTypeList()]
    }
    
    def productTypeUpdate = { 
        println 'product type update '
        
        def id = params['id']
        
        ProductType pt = getSingleProductType(id)
        
        [productTypeId:id, productTypeName:pt.productTypeName, productTypeDescription:pt.description]
    }
    
    def productTypeDelete = { 
        println 'product type delete '
    }
    
    
    
    /* Wine Product */
    def wineProductCreate = { 
        println 'wine product create '
        
        [productTypes:getProductTypeList(), wineTypes:getWineTypeList(), wineVarietals:getWineVarietalList(), wineRegions:getWineRegionList()]
    }
    
    def wineProductRetrieve = { 
        println 'wine product Retrieve '
        
        [wineProducts:getProductList()]
    }
    
    def wineProductUpdate = { 
        println 'wine product update '
        
        def id = params['id']
        
        Product p = getSingleProduct(id)
        println "the product found is : "
        println p.productName
        println p.productTypeName
        ArrayList<ProductOption> poList = getProductOptionList(id)
        
        // wine options
        def wineTypeName = ""
        def wineVarietalName = ""
        def wineRegionName = ""
        ArrayList wineVarietals = new ArrayList()
        
        poList.each() {
            if (it.productId == p.id) {
                if (it.optionType.equals("wineType")) {
                    WineType wt = getSingleWineType(it.optionTypeId)
                    wineTypeName = wt.wineTypeName
                    println 'has winetype'
                    // get first list of wine varietals of the white type
                    WineType wineType = getSingleWineType(wineTypeName)
                    def currentWineVarietalList = getWineVarietalList()
                    currentWineVarietalList.each() {           
                        if (it.wineTypeName.equals(wineTypeName)) {
                            wineVarietals.add(it)
                        }
                    }
                }
                
                if (it.optionType.equals("wineVarietal")) {
                    WineVarietal wv = getSingleWineVarietal(it.optionTypeId)
                    wineVarietalName = wv.wineVarietalName
                    println 'has wineVarietal'
                }
                
                if (it.optionType.equals("wineRegion")) {
                    WineRegion wr = getSingleWineRegion(it.optionTypeId)
                    wineRegionName = wr.wineRegionName
                    println 'has wineRegion'
                }
            }
        }
        
        [productId:p.id, productName:p.productName, productDescription:p.description, productImageUrl:p.imageUrl, productType:p.productTypeName, wineTypes:getWineTypeList(), wineVarietalsOnce: wineVarietals, wineRegions:getWineRegionList(), wineTypeName:wineTypeName, wineVarietalName:wineVarietalName, wineRegionName:wineRegionName]
    }
    
    
    
    
    
    // REST-related operations for accessing WineXpress Server (for clearer view only)
    // GET
    def show = {
        
    }
    
    // PUT  
    def updateWineType = {
        def id = params['id']
        def wineTypeName = params['wineTypeName']        
        def description = params['description']
        
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("wineTypeName",wineTypeName)
        o.put("description",description)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/wineType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateWineType OK"
        
        flash.message = "Wine type has been successfully updated."
        
        redirect(uri:"/wineTypeRetrieve")
    }
    
    def updateWineVarietal = {
        def id = params['id']
        def wineVarietalName = params['wineVarietalName']        
        def description = params['description']
        def wineType = params['wineType']
        
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("wineVarietalName",wineVarietalName)
        o.put("description",description)
        o.put("wineType",wineType)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/wineVarietal") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateWineVarietal OK"
        
        flash.message = "Wine varietal has been successfully updated."
        
        redirect(uri:"/wineVarietalRetrieve")
    }
    
    def updateWineRegion = {
        def id = params['id']
        def wineRegionName = params['wineRegionName']        
        def description = params['description']
//        def country = params['country']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("wineRegionName",wineRegionName)
        o.put("description",description)
//        o.put("country",country)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/wineRegion") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateWineRegion OK"
        
        flash.message = "Wine region has been successfully updated."
        
        redirect(uri:"/wineRegionRetrieve")
    }
    
    def updateProductType = {
        def id = params['id']
        def productTypeName = params['productTypeName']        
        def description = params['description']
        
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("productTypeName",productTypeName)
        o.put("description",description)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/productType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateProductType OK"
        
        flash.message = "Product type has been successfully updated."
        
        redirect(uri:"/productTypeRetrieve")
    }
    
    def updateWineProduct = {
        def id = params['id']
        def productName = params['productName']        
        def description = params['description']
        def productType = params['productType']
//        def imageUrl = request.getFile('productImage')
//        println imageUrl
        def wineType = params['wineType']
        def wineVarietal = params['wineVarietal']
        def wineRegion = params['wineRegion']
        
        println 'productName :: '  + productName
        println 'description :: '  + description
        println 'productType :: '  + productType
        println 'wineType :: '  + wineType
        println 'wineVarietal :: '  + wineVarietal
        println 'wineRegion :: '  + wineRegion
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("productName",productName)
        o.put("description",description)
        o.put("productType",productType)
//        o.put("imageUrl",imageUrl)
        if (productType.equals("Wine")) {
            println 'yes it is wine product so add the options!'
            o.put("wineType",wineType)
            o.put("wineVarietal",wineVarietal)
            o.put("wineRegion",wineRegion)
        }
        if (productType.equals("Gift")) {
//            o.put("wineType",wineType)
//            o.put("wineVarietal",wineVarietal)
//            o.put("wineRegion",wineRegion)
        }
        if (productType.equals("Hamper")) {
//            o.put("wineType",wineType)
//            o.put("wineVarietal",wineVarietal)
//            o.put("wineRegion",wineRegion)
        }
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/product") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 200) {
            flash.message = "Product has been successfully updated."
        }
        if (statusCode == 400) {
            flash.message = "Product could not be created. Please try again."
        }
        
        redirect(uri:"/wineProductRetrieve")
    }
    
    // DELETE
    def delete = {
        
    }
    
    def wineProductDelete = { 
        println 'wine product delete '
    }
    
    
    
    // POST
    def postCreateWineType = {
        println 'postCreateWineType'
        // get form values
        def wineTypeName = params['wineTypeName']        
        def description = params['wineTypeDescription']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("wineTypeName",wineTypeName)
        o.put("description",description)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);

        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            flash.message = "Wine type successfully created."
            try {
                redirect(uri:"/wineTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 302) {
            flash.message = "Wine type already exists."
            try {
                redirect(uri:"/wineTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 400) {
            flash.message = "Wine type could not be created. Please try again."
            try {
                redirect(uri:"/wineTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 406) {
            flash.message = "Wine type already exists."
            try {
                redirect(uri:"/wineTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        
    }
    
    def postCreateWineVarietal = {
        println 'postCreateWineVarietal'
        // get form values
        def wineVarietalName = params['wineVarietalName']        
        def description = params['wineVarietalDescription']
        def wineType = params['wineType']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("wineVarietalName",wineVarietalName)
        o.put("description",description)
        o.put("wineType",wineType)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineVarietal") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            flash.message = "Wine varietal successfully created."
            try {
                redirect(uri:"/wineVarietalCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 302) {
            flash.message = "Wine varietal already exists."
            try {
                redirect(uri:"/wineVarietalCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 400) {
            flash.message = "Wine varietal could not be created. Please try again."
            try {
                redirect(uri:"/wineVarietalCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 406) {
            flash.message = "Wine varietal already exists."
            try {
                redirect(uri:"/wineVarietalCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
    }
    
    def postCreateWineRegion = {
        println 'postCreateWineVarietal'
        // get form values
        def wineRegionName = params['wineRegionName']        
        def description = params['wineRegionDescription']
        def country = params['country']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("wineRegionName",wineRegionName)
        o.put("description",description)
        o.put("country",country)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineRegion") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            flash.message = "Wine region successfully created."
            try {
                redirect(uri:"/wineRegionCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 302) {
            flash.message = "Wine region already exists."
            try {
                redirect(uri:"/wineRegionCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 400) {
            flash.message = "Wine region could not be created. Please try again."
            try {
                redirect(uri:"/wineRegionCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 406) {
            flash.message = "Wine region already exists."
            try {
                redirect(uri:"/wineRegionCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
    }
    
    def postCreateProductType = {
        println 'postCreateProductType'
        // get form values
        def productTypeName = params['productTypeName']        
        def description = params['productTypeDescription']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("productTypeName",productTypeName)
        o.put("description",description)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/productType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            flash.message = "Product type successfully created."
            try {
                redirect(uri:"/productTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 302) {
            flash.message = "Product type already exists."
            try {
                redirect(uri:"/productTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 400) {
            flash.message = "Product type could not be created. Please try again."
            try {
                redirect(uri:"/productTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 406) {
            flash.message = "Product type already exists."
            try {
                redirect(uri:"/productTypeCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
    }
    
    def postCreateWineProduct = {
        println 'postCreateWineProduct'
        // get form values
        def productName = params['productName']        
        def description = params['description']
        def productType = params['productType']
//        def imageUrl = request.getFile('productImage')
//        println imageUrl
        def wineType = params['wineType']
        def wineVarietal = params['wineVarietal']
        def wineRegion = params['wineRegion']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("productName",productName)
        o.put("description",description)
        o.put("productType",productType)
//        o.put("imageUrl",imageUrl)
        if (productType.equals("Wine")) {
            o.put("wineType",wineType)
            o.put("wineVarietal",wineVarietal)
            o.put("wineRegion",wineRegion)
        }
        if (productType.equals("Gift")) {
//            o.put("wineType",wineType)
//            o.put("wineVarietal",wineVarietal)
//            o.put("wineRegion",wineRegion)
        }
        if (productType.equals("Hamper")) {
//            o.put("wineType",wineType)
//            o.put("wineVarietal",wineVarietal)
//            o.put("wineRegion",wineRegion)
        }
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/product/wine") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            flash.message = "Product successfully created."
            try {
                redirect(uri:"/wineProductCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 302) {
            flash.message = "Product already exists."
            try {
                redirect(uri:"/wineProductCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 400) {
            flash.message = "Product could not be created. Please try again."
            try {
                redirect(uri:"/wineProductCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
        if (statusCode == 406) {
            flash.message = "Product already exists."
            try {
                redirect(uri:"/wineProductCreate")
            }
            catch (CannotRedirectException) {
                println 'already redirected...'
            }
        }
    }
    
    
    
    
    // reusable methods
    
   
}
